package com.yunji.web.system;

import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.model.domain.SysDictDataDO;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.common.utils.StringUtils;
import com.yunji.system.context.SystemContext;
import com.yunji.system.service.ISysDictDataService;
import com.yunji.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation("数据字典列表")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public Response<List<SysDictDataDO>> list(SysDictDataDO dictData) {
        List<SysDictDataDO> list = dictDataService.selectDictDataList(dictData);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("字典数据导出")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictDataDO dictData) {
        List<SysDictDataDO> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictDataDO> util = new ExcelUtil<SysDictDataDO>(SysDictDataDO.class);
        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @ApiOperation("查询字典数据详细")
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public Response<SysDictDataDO> getInfo(@PathVariable Long dictCode) {
        return ResponseHelper.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public Response<List<SysDictDataDO>> dictType(@PathVariable String dictType) {
        List<SysDictDataDO> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<>();
        }
        return ResponseHelper.success(data);
    }

    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public Response<Long> add(@Validated @RequestBody SysDictDataDO dict) {
        dict.setCreateBy(SystemContext.getUsername());
        Assert.isTrue(dictDataService.insertDictData(dict) > 0, "新增失败");
        return ResponseHelper.success(dict.getDictCode());
    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation("修改保存字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response<String> edit(@Validated @RequestBody SysDictDataDO dict) {
        dict.setUpdateBy(SystemContext.getUsername());
        Assert.isTrue(dictDataService.updateDictData(dict) > 0, "修改失败");
        return ResponseHelper.success();
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public Response<String> remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return ResponseHelper.success();
    }
}
